import service from '@/utils/request'
import { Message } from 'element-ui'
service.interceptors.request.use(
  config => {
      console.log(config);
      
      // config.headers['X-Authorization'] = 'Bearer ' + Cookies.get('token')
      // config.headers.Authorization= 'Bearer ' + Cookies.get('token')
    return config
  },
  error => {
    console.log(error) // for debug
    return Promise.reject(error)
  }
)
service.interceptors.response.use(
  response => {
    
    const res = response
    console.log(response);
    
    return res
  },
  error => {
    console.log('err' + error) // for debug
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service
export function fetchMyData(url, params, data={}, type = 'post') {
  let config = {
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'X-Authorization': 'Bearer ' + Cookies.get('token')
    }
  };
  return new Promise((resolve, reject) => {
    service({
      method: type,
      url: url,
      data: data,
      headers: config.headers,
      params: params,
      dataType: "json",
      withCredentials: false
    }).then((response) => {
      resolve(response.data)
    }, err => {
      reject(err)
    }).catch((err) => {
      reject(err)
    })
  })
}